package com.dc.seckill.service;

import com.dc.seckill.dao.GoodsDao;
import com.dc.seckill.domain.MiaoshaGoods;
import com.dc.seckill.exception.GlobalException;
import com.dc.seckill.result.CodeMsg;
import com.dc.seckill.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @模块 商品模块
 * @作者 djs
 * @时间 2018/3/21 8:46
 * @版本 V1.0
 * @描述 提供处理商品信息的方法
 */
@Service
public class GoodsService {

    private Logger log = LoggerFactory.getLogger(GoodsService.class);

    @Autowired
    GoodsDao goodsDao;

    /**
     * 查询所有的秒杀商品
     * @params []
     * @returns java.util.List<com.dc.seckill.vo.GoodsVo>
     */
    public List<GoodsVo> listGoodsVo(){
        return goodsDao.listGoodsVo();
    }

    /**
     * 根据商品ID获取秒杀信息
     * @params [goodsId]
     * @returns com.dc.seckill.vo.GoodsVo
     */
    public GoodsVo queryGoodsVoByGoodsID(Long goodsId){
        if(goodsId == null){
            throw new GlobalException(CodeMsg.BIND_ERROR.fillArgs("商品ID不能为空"));
        }
        return goodsDao.queryGoodsVoByGoodsID(goodsId);
    }

    /**
     * 进行减库存
     * @params [goods]
     * @returns boolean
     */
    public boolean reduceStock(GoodsVo goods) {
        MiaoshaGoods miaoshaoGoods = new MiaoshaGoods();
        miaoshaoGoods.setGoodsId(goods.getId());
        int result = goodsDao.reduceStock(miaoshaoGoods);
        log.info("减库存操作结果为:"+result);
        if(result > 0){
            return true;
        }
        return false;
    }
}
